"use strict"
var info, botm, isClsPicOpen, slide;
function init() {//初始化
    info = M.Modal.init(document.getElementById("modal"), {
        onCloseEnd: function () {
            document.getElementById("info_content").innerHTML = '';
        },
    });
    botm = M.Modal.init(document.getElementById("modalb"), {
        onCloseEnd: function () {
            document.getElementById("botm_content").innerHTML = '';
            if (isClsPicOpen) {
                ClsPicOff()
            }
        }
    });
    M.Sidenav.init(document.getElementById("slide-out"), {});
    isClsPicOpen = false;
    window.addEventListener("hashchange", hchanged, false)

    //根据location.hash打开对应的框框和页面
    switch (location.hash) {
        case "#tab/2": {
            ctab('tab2', '小说')
            break;
        }
        case "#tab/3": {
            ctab('tab3', '小游戏')
            break;
        }
        case "#tab/4": {
            ctab('tab4', '关于和聊天')
            break;
        }
        default: {
            if (location.hash.split("/")[0] == "#modal") {
                modal(location.hash.substring(7, location.hash.length));
            } else if (location.hash.split("/")[0] == "#reader") {
                reader(location.hash.substring(8, location.hash.length));
            }
            break;
        }
    }

    //更改软件下载页面
    var xhr = new XMLHttpRequest();
    xhr.open("GET", './indexs/software.json', true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                let ans = JSON.parse(xhr.responseText);
                var tab2='<div class="row">'
                for (var abb in ans) {
                    tab2+='<div class="col s12 m6 l4"><div class="card"><div class="card-image"><img src="img/'+ans[abb].pic+'"><span class="card-title" style="color: white;">'+ans[abb].name+'</span></div><div class="card-content"><p>'+ans[abb].info+'</p></div><div class="card-action"><a href="#modal/noval/acted_mainline" onclick="modal(\''+abb+'/help\')">软件教程</a><a href="#modal/noval/acted_mainline" onclick="modal(\''+abb+'/download\')">软件下载</a><a href="'+ans[abb]["origin-site"]+'">软件原网址</a></div></div> </div>'
                }
                tab2 +='</div>'
                document.getElementById("tab2").innerHTML=tab2;
            } else {
                alert("网络错误，代码" + xhr.status);
            }
        }
    }

}

function hchanged() {
    console.log(location.hash);
    if (location.hash == "#c") {//关阅读器+关modal框框
        if (isClsPicOpen) {
            ClsPicOff();
        }
        info.close();
    }
}

function ClsPicOn(id, blurpic, word) { //新的阅读器
    //删掉其他内容
    document.getElementById("slide-out").style.display = "none"
    document.getElementById("main").style.display = "none"
    document.getElementById("footer").style.display = "none"
    if (isClsPicOpen) {//如果打开了图片的话
        console.log("图片已打开，只进行改变")
        if (document.getElementById("test-active-bkgdiv").style.backgroundImage != "url(\"./img/" + id + ".jpg\")") { //并检测图片是否更改过。如更改过，渐变更改为另一个
            document.getElementById("test-active-bkgdiv").style.animation = "fadeaway 0.3s forwards"
            setTimeout(() => {//填入新图片并显示
                document.getElementById("test-active-bkgdiv").style.backgroundImage == "url(\"./img/" + id + ".jpg\")";
                document.getElementById("test-active-bkgdiv").style.animation = "fadein 0.3s forwards"
            }, 301);
        }
        if (word != document.getElementById("test-active").innerHTML) {//再检测文字改变过没有，改变过的话渐变
            document.getElementById("test-active").style.animation += "fadeaway 0.3s forwards"
            setTimeout(() => {//填入新文字并显示
                document.getElementById("test-active").innerHTML = '<div class="reader">' + word + '<div class="row center"><button class="waves-effect waves-light btn-flat" onclick="ClsPicOff()">关闭</button></div></div>';
                document.getElementById("test-active").style.animation = "fadein 0.3s forwards"
                setTimeout(() => {
                    document.getElementById("test-active").style.animation = "";
                }, 300);
                document.getElementById("test-active").scrollTo(0, 0);
            }, 301);
        }
    } else {
        //关掉小把手
        document.getElementById("slide-menu").style.display = "none"
        //提交图片打开了的提示
        isClsPicOpen = true;
        if (word != null && word != undefined) {
        //设置文字
        document.getElementById("test-active").innerHTML = '<div class="reader">' + word + '<div class="row center"><button class="waves-effect waves-light btn-flat" onclick="ClsPicOff()">关闭</button></div></div>';
        document.getElementById("test-active").style.display = "block";
        document.getElementById("test-active").scrollTo(0, 0);
        }
        //图片从底部弹出
        document.getElementById("test-active-bkgdiv").style.left = "0px";
        document.getElementById("test-active-bkgdiv").style.top = document.defaultView.getComputedStyle(document.body, null)["height"];
        //文字从底部弹出
        document.getElementById("test-active").style.left = "0px";
        document.getElementById("test-active").style.top = document.defaultView.getComputedStyle(document.body, null)["height"];
        //设置图片
        document.getElementById("test-active-bkgdiv").style.backgroundImage = "url(\"./img/" + id + ".jpg\")"
        document.getElementById("test-active-bkgdiv").style.display = "block";
        //图片的动画
        document.getElementById("test-active-bkgdiv").style.animation += "0.5s cubic-bezier(.56,-0.18,.29,.94) 0s 1 normal forwards running jumpin";
        setTimeout(() => {
            document.getElementById("test-active-bkgdiv").style.top = "0px";//保留第一个动画的成果
            if (blurpic == "false" || blurpic == false || blurpic === undefined || blurpic === null) { //判断是否模糊
                document.getElementById("test-active-bkgdiv").style.animation = "blurin 0.1s forwards";
                setTimeout(() => {//保留模糊结果
                    document.getElementById("test-active-bkgdiv").style.filter = "blur(10px)";
                    document.getElementById("test-active-bkgdiv").style.animation = "";
                }, 101);
            } else {
                document.getElementById("test-active-bkgdiv").style.animation = ""
            }
        }, 501);
        setTimeout(function () {//文字框延时出现
            if (word != null && word != undefined) { //如果使用自己做的显示框框
                document.getElementById("test-active").style.animation = "0.5s cubic-bezier(.56,-0.18,.29,.94) 0s 1 normal forwards running jumpin";
                setTimeout(() => {//保留定位结果
                    document.getElementById("test-active").style.top = "0px"
                    document.getElementById("test-active").style.animation = ""
                }, 501);
            }
        }, 10);
    }
}

function ClsPicOff() {//关掉拟态框
    if (isClsPicOpen) {

        //设置图片打开标识符
        isClsPicOpen = false;
        if (document.getElementById("test-active-bkgdiv").style.filter.indexOf("blur") > -1) { //判断是否有模糊
            document.getElementById("test-active-bkgdiv").style.animation = "blurout 0.1s forwards";
            setTimeout(() => {//移除模糊
                document.getElementById("test-active-bkgdiv").style.filter = "";
                document.getElementById("test-active-bkgdiv").style.animation = "";
                document.getElementById("test-active").style.animation = "0.5s cubic-bezier(.56,-0.18,.29,.94) 0s 1 normal forwards running jumpout";
                setTimeout(function () {//背景后退
                    document.getElementById("test-active-bkgdiv").style.animation = "0.5s cubic-bezier(.56,-0.18,.29,.94) 0s 1 normal forwards running jumpout";
                    setTimeout(() => {//显示主页面
                        mainout();
                    }, 501);
                }, 10);
            }, 101);
        } else {
            document.getElementById("test-active").style.animation = "0.5s cubic-bezier(.56,-0.18,.29,.94) 0s 1 normal forwards running jumpout";
            setTimeout(function () {//背景后退
                document.getElementById("test-active-bkgdiv").style.animation = "0.5s cubic-bezier(.56,-0.18,.29,.94) 0s 1 normal forwards running jumpout";
                setTimeout(() => {//显示主页面
                    mainout();
                }, 500);
            }, 10);
        }
    }
    function mainout() {  //主页面出现
        //弹出框的初始化
        document.getElementById("test-active").style.animation = "";
        document.getElementById("test-active-bkgdiv").style.animation = "";
        document.getElementById("test-active").style.display = "none";
        document.getElementById("test-active-bkgdiv").style.display = "none";
        document.getElementById("test-active").innerHTML = "";
        //侧边栏
        document.getElementById("slide-out").style.display = "block"
        document.getElementById("slide-out").style.animation = "fadein 0.3s forwards";
        setTimeout(() => {
            document.getElementById("slide-out").style.animation = "";

        }, 301);
        //主内容
        document.getElementById("main").style.display = "block"
        document.getElementById("main").style.animation = "fadein 0.3s forwards";
        setTimeout(() => {
            document.getElementById("main").style.animation = "";
        }, 301);
        //底部
        document.getElementById("footer").style.display = "block"
        document.getElementById("footer").style.animation = "fadein 0.3s forwards";
        setTimeout(() => {
            document.getElementById("footer").style.animation = "";
        }, 301);
        //小把手
        if (screen.width < 992)
            document.getElementById("slide-menu").style.display = "block"
        document.getElementById("slide-menu").style.animation = "fadein 0.3s forwards";
        setTimeout(() => {
            document.getElementById("slide-menu").style.animation = "";
        }, 301);
    }
}

function modal(node, jscode) { //拟态框页面
    var xhr = new XMLHttpRequest();
    xhr.open("GET", './modals/' + node + '.json', true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                location.hash = "#c"
                location.hash = "#modal/" + node;
                let ans = JSON.parse(xhr.responseText);
                document.getElementById("info_header").innerHTML = ans.header;
                document.getElementById("info_content").innerHTML = ans.content;
                document.getElementById("info_footer").innerHTML = ans.footer;
                info.open();
                if (jscode !== null && jscode !== undefined) { //如果有异步操作
                    jscode.apply();
                }
            } else {
                alert("网络错误，代码" + xhr.status);
            }
        }
    }
}
function reader(node, jscode) {//新的阅览器,支持Markdown啦
    localStorage.setItem("lastread", node)
    var xhr = new XMLHttpRequest();
    xhr.open("GET", './reader/' + node + '.json', true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                let ans = JSON.parse(xhr.responseText);
                if (ans.isMarkDown == "true" || ans.isMarkDown == true) {
                    ClsPicOn(ans.bgpic, localStorage.getItem("setting-pic"), marked(ans.content));
                } else {
                    ClsPicOn(ans.bgpic, localStorage.getItem("setting-pic"), ans.content)
                }

                location.hash = "#c"
                location.hash = "#reader/" + node;
                if (jscode !== null && jscode !== undefined) { //如果有异步操作
                    jscode.apply();
                }
            } else {
                alert("网络错误，代码" + xhr.status);
            }
        }
    }
}

function SettingSet(name, value) { //设置保存
    if (value === undefined || value === null) {
        localStorage.removeItem(name);
    } else {
        localStorage.setItem(name, value);
    }
}

function SettingCKBLoad(name, id) { //开关设置内容加载
    if (localStorage.getItem(name) == true || localStorage.getItem(name) == "true") {
        document.getElementById(id).checked = "1"
    } else {
        document.getElementById(id).checked = ""
    }
}
function ctab(tab, title) {//动画更换页面
    if (title !== null && title !== undefined && title != "") {
        if (document.getElementById("NavTitle").innerHTML == "") {
            document.getElementById("top-nav").style.display = "block";
            document.getElementById("NavTitle").innerHTML = title;
            document.getElementById("NavTitle").style.animation = "fadein 0.3s forwards";
            setTimeout(function () {
                document.getElementById("NavTitle").style.animation = "";
            }, 301);
        } else {
            document.getElementById("NavTitle").style.animation = "fadeaway 0.3s forwards";
            setTimeout(function () {
                document.getElementById("NavTitle").innerHTML = title;
                document.getElementById("NavTitle").style.animation = "fadein 0.3s forwards";
            }, 301);
            setTimeout(function () {
                document.getElementById("NavTitle").style.animation = "";
            }, 601);
        }
    } else {
        document.getElementById("NavTitle").style.animation = "fadeaway 0.3s forwards";
        setTimeout(function () {
            document.getElementById("NavTitle").style.animation = "";
            document.getElementById("top-nav").style.display = "none";
            document.getElementById("NavTitle").innerHTML = "";
        }, 301);
    }


    document.getElementById("tab1").style.animation = "fadeaway 0.3s forwards";
    document.getElementById("tab2").style.animation = "fadeaway 0.3s forwards";
    document.getElementById("tab3").style.animation = "fadeaway 0.3s forwards";
    document.getElementById("tab4").style.animation = "fadeaway 0.3s forwards";

    setTimeout(function () {
        document.getElementById("tab1").style.display = "none";
        document.getElementById("tab2").style.display = "none";
        document.getElementById("tab3").style.display = "none";
        document.getElementById("tab4").style.display = "none";

        document.getElementById(tab).style.display = "block";
        document.getElementById(tab).style.animation = "fadein 0.3s forwards";
    }, 301);
    setTimeout(function () {
        document.getElementById("tab1").style.animation = "";
        document.getElementById("tab2").style.animation = "";
        document.getElementById("tab3").style.animation = "";
        document.getElementById("tab4").style.animation = "";
    }, 601);
}
function lastread() {//继续上次的阅读
    if (localStorage.getItem("lastread")) {
        reader(localStorage.getItem("lastread"));
    } else {
        alert("看起来你还没有读过呢，试试手气!")
        randomread()
    }
}
function randomread() {//随机来个软件？
    var xhr = new XMLHttpRequest();
    xhr.open("GET", './indexs/software.json', true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                let ans = JSON.parse(xhr.responseText);
                let num = Object.keys(ans).length;
                reader(Object.keys(ans)[Math.ceil(Math.random() * num - 1)] + "/0");//读取第一篇
            } else {
                alert("网络错误，代码" + xhr.status);
            }
        }
    }
}
